<?php

namespace backend\controllers;

use Yii;
use backend\models\AdminUser;
use backend\models\AdminUserSearch;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * AdminUserController implements the CRUD actions for AdminUser model.
 */
class AdminUserController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors ()
    {
        return [
            'verbs' => [
                'class'   => VerbFilter::className() ,
                'actions' => [
                    'delete' => [ 'POST' ] ,
                ] ,
            ] ,
        ];
    }

    /**
     * @return string
     *
     * 用户列表
     */
    public function actionIndex ()
    {

        $searchModel = new AdminUserSearch();
        $dataProvider = $searchModel->search( Yii::$app->request->queryParams );
        return $this->render( 'index' , [
            'searchModel'  => $searchModel ,
            'dataProvider' => $dataProvider ,
        ] );
    }

    /**
     * @return string|\yii\web\Response
     *
     * 创建
     */
    public function actionCreate ()
    {
        $model = new AdminUser();
        if ( $model->load( Yii::$app->request->post() ) ) {
            $post = Yii::$app->request->post();
            if( $post['AdminUser']['type'] == 3 ){
                $model->nickname = '操作员';
                $model->is_pc     = 1;
                $model->is_mobile = 2;
                $model->is_audit  = 1;
                $model->is_check_project   = 1;
                $model->is_edit_project    = 1;
                $model->is_edit_notice     = 1;
                $model->is_create_account  = 2;
            }
            if( $post['AdminUser']['type'] == 4 ){
                $model->nickname = '财务';
                $model->is_pc     = 1;
                $model->is_mobile = 2;
                $model->is_audit  = 2;
                $model->is_check_project   = 1;
                $model->is_edit_project    = 2;
                $model->is_edit_notice     = 2;
                $model->is_create_account  = 2;
            }
            $model->password = password_hash( $post['AdminUser']['password'],PASSWORD_DEFAULT);
            $model->ctime = date('Y-m-d H:i:s',time());
            if($model->save()){
                return $this->success( [ 'index' ] );
            };
        } else {
            return $this->render( 'create' , [
                'model' => $model ,
//                'role'  => $this->getRole() ,
            ] );
        }
    }

    /**
     * @param $id
     *
     * @return string|\yii\web\Response
     * @throws NotFoundHttpException
     *
     * 更新
     */
    public function actionUpdate ( $id )
    {
        $model = $this->findModel( $id );
        $model->password = '';

        if ( $model->load( Yii::$app->request->post() ) ) {
            $post = Yii::$app->request->post();
            if( $post['AdminUser']['type'] == 2 ){
                $model->nickname = '项目负责人';
            }
            if( $post['AdminUser']['type'] == 3 ){
                $model->nickname = '操作员';
            }
            if( $post['AdminUser']['type'] == 4 ){
                $model->nickname = '财务';
            }

            $model->password = password_hash($post['AdminUser']['password'],PASSWORD_DEFAULT);
            if($model->save()){
                return $this->success( [ 'index' ] );
            }
        } else {
            return $this->render( 'update' , [
                'model' => $model ,
//                'role'  => $this->getRole() ,
            ] );
        }
    }
    /**
     * @param $id
     *
     * @return string|\yii\web\Response
     * @throws NotFoundHttpException
     *
     * 更新
     */
    public function actionAuth ( $id )
    {
        $model = $this->findModel( $id );
        if ( $model->load( Yii::$app->request->post() ) && $model->save() ) {
            return $this->success( [ 'index' ] );
        }else{
            return $this->render( 'auth' , [
                'model' => $model ,
            ] );
        }

    }

    /**
     * Deletes an existing AdminUser model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionDelete ( $id )
    {
        $ret = AdminUser::updateAll(['status'=> 9],['id'=>$id]);
        if($ret){
            return $this->success( [ 'index' ] );
        }
    }

    /**
     * Finds the AdminUser model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     *
     * @param integer $id
     *
     * @return AdminUser the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel ( $id )
    {
        if ( ( $model = AdminUser::findOne( $id ) ) !== NULL ) {
            return $model;
        } else {
            throw new NotFoundHttpException( 'The requested page does not exist . ' );
        }
    }

}
